using System;
using System.Data;
using System.Drawing.Imaging;
using System.IO;


public partial class Vertical_WeekView : System.Web.UI.Page
{
    private DataTable table;

    protected void Page_Load(object sender, EventArgs e)
    {
        #region Data loading initialization
        if (Session["DaysModeWeek"] == null)
        {
            Session["DaysModeWeek"] = DataGeneratorScheduler.GetData();
        }
        table = (DataTable)Session["DaysModeWeek"];
        DayPilotScheduler1.DataSource = Session["DaysModeWeek"];
        #endregion
        
        if (!IsPostBack)
        {
            DayPilotScheduler1.StartDate = DayPilot.Utils.Week.FirstDayOfWeek(DateTime.Today);
            DayPilotScheduler1.SetScrollX(DayPilotScheduler1.StartDate.Date.AddHours(9));
            DataBind();
        }

    }
    protected void DayPilotScheduler1_EventResize(object sender, DayPilot.Web.Ui.Events.EventResizeEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.Rows.Find(e.Value);
        if (dr != null)
        {
            dr["start"] = e.NewStart;
            dr["end"] = e.NewEnd;
            table.AcceptChanges();
        }

        #endregion

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();
    }
    protected void DayPilotScheduler1_EventMove(object sender, DayPilot.Web.Ui.Events.EventMoveEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.Rows.Find(e.Value);
        if (dr != null)
        {
            dr["start"] = e.NewStart;
            dr["end"] = e.NewEnd;
            dr["column"] = e.NewResource;
            table.AcceptChanges();
        }

        #endregion

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();
    }

    protected void DayPilotScheduler1_TimeRangeSelected(object sender, DayPilot.Web.Ui.Events.TimeRangeSelectedEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.NewRow();
        dr["start"] = e.Start;
        dr["end"] = e.End;
        dr["id"] = Guid.NewGuid().ToString();
        dr["name"] = "New event";

        table.Rows.Add(dr);
        table.AcceptChanges();
        #endregion

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DayPilotScheduler1.DataBind();

        Response.Clear();
        Response.ContentType = "image/png";
        Response.AddHeader("content-disposition", "attachment;filename=print.png");
        DayPilotScheduler1.DataBind();
        MemoryStream img = DayPilotScheduler1.Export(ImageFormat.Png);
        img.WriteTo(Response.OutputStream);
        Response.End();

    }

    protected void DayPilotScheduler1_BeforeResHeaderRender(object sender, DayPilot.Web.Ui.Events.BeforeHeaderRenderEventArgs e)
    {
        /*
        if (e.IsCorner)
        {
            e.InnerHTML = e.Date.ToShortDateString();
        }*/
    }


}
